//
//  WCILockAlertVC.m
//  WCImApp
//
//  Created by Hyukooooh on 2024/4/8.
//  Copyright © 2024 WCImApp. All rights reserved.
//

#import "WCILockAlertVC.h"

@interface WCILockAlertVC ()
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, assign) NSTimeInterval count;

@property (nonatomic, assign) BOOL hasShow;
@end

@implementation WCILockAlertVC
- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self stopCount];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.hasShow) {return;}
    self.hasShow = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.3];
    }];
    
    [self.view addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(240);
        make.centerX.centerY.equalTo(self.view);
    }];
    [self.view layoutIfNeeded];
    MOLockAlertContentView *containerView = (MOLockAlertContentView *)self.containerView;
    if (self.count > 0) {
        NSString *countStr = [NSString stringWithFormat:@"%lds",(NSInteger)self.count];
        NSString *desccontent = [NSString stringWithFormat:(@"密码连续错误达到设定次数\n %@ 后即将自动注销吉信账号"), countStr];
        NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc]initWithString:desccontent];
        NSArray *rangeArray1 = [NSString rangeOfSubString:countStr inString:desccontent];
        for (NSValue *ran in rangeArray1) {
            NSRange range = [ran rangeValue];
            if (range.length + range.location <= attr1.length) {
                [attr1 addAttribute:NSForegroundColorAttributeName value:[UIColor assist_Warning_Color] range:range];
            }
        }
        containerView.contentLabel.attributedText = attr1;
        [self startCount];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)startCount {
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopCount {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)countDown:(NSTimer *)timer {
    MOLockAlertContentView *containerView = (MOLockAlertContentView *)self.containerView;
    if (self.count <= 1) {
        [self stopCount];
        if(containerView.btnCallback){
            containerView.btnCallback(1);
        }
        return;
    }
    self.count -= 1;

    NSString *countStr = [NSString stringWithFormat:@"%lds",(NSInteger)self.count];
    NSString *desccontent = [NSString stringWithFormat:(@"密码连续错误达到设定次数\n %@ 后即将自动注销吉信账号"), countStr];
    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc]initWithString:desccontent];
    NSArray *rangeArray1 = [NSString rangeOfSubString:countStr inString:desccontent];
    for (NSValue *ran in rangeArray1) {
        NSRange range = [ran rangeValue];
        if (range.length + range.location <= attr1.length) {
            [attr1 addAttribute:NSForegroundColorAttributeName value:[UIColor assist_Warning_Color] range:range];
        }
    }
    containerView.contentLabel.attributedText = attr1;
}
+ (void)presentAlertViewFromViewController:(UIViewController *)fromViewController
                                   content:(NSString *)content
                                     count:(NSTimeInterval)count
                               btnCallback:(void(^ _Nullable)(NSUInteger))banCallback {
    WCILockAlertVC *alertCtr = [[WCILockAlertVC alloc] init];
    alertCtr.count = count;
    MOLockAlertContentView *commonContainer = [[MOLockAlertContentView alloc] initWithFrame:CGRectZero];
    commonContainer.contentLabel.text = content ?: @"";
    __weak typeof(alertCtr)weakAlertCtr = alertCtr;
    commonContainer.btnCallback = ^(NSUInteger index) {
        banCallback ? banCallback(index) : nil;
        [weakAlertCtr.view removeFromSuperview];
        [weakAlertCtr removeFromParentViewController];
    };
    alertCtr.containerView = commonContainer;
    alertCtr.view.backgroundColor = [UIColor clearColor];
    [fromViewController addChildViewController:alertCtr];
    [alertCtr didMoveToParentViewController:fromViewController];
    alertCtr.view.frame = [UIScreen mainScreen].bounds;
    [fromViewController.view addSubview:alertCtr.view];
    [fromViewController.view bringSubviewToFront:alertCtr.view];
}
@end

@implementation MOLockAlertContentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 20;
    self.layer.masksToBounds = YES;
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectZero];
    imgV.image = [UIImage imageNamed:@"icon_lock_alert_error"];
    self.tipsImgV = imgV;
    [self addSubview:imgV];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(48);
        make.top.equalTo(self).offset(28);
        make.centerX.equalTo(self);
    }];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.contentLabel = contentLabel;
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = [UIColor assist_ClickText_color];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32);
        make.right.equalTo(self).offset(-32);
        make.top.equalTo(imgV.mas_bottom).offset(11);
        make.bottom.equalTo(self).offset(-30);
    }];
}


@end
